---
---
<!DOCTYPE html>
<html lang=en>

<head>
    <meta charset=utf-8>

    <link rel="stylesheet" type="text/css" href="{{ site.baseurl }}/common/css/stylesheet.css"/>
    <link rel="stylesheet" href="{{ site.baseurl }}/common/css/stylesheet-fonts.css" type="text/css" charset="utf-8">
    <link rel="icon" href="{{ site.baseurl }}/common/css/favicon.ico" type="image/x-icon">
    <link rel="shortcut icon" href="{{ site.baseurl }}/common/css/favicon.ico" type="image/x-icon">
    <title> Magento for Developers: Part 2&mdash;The Magento Config</title>

</head>
<body>
<a name="top"></a>

<img src="{{ site.baseurl }}/common/images/m1x/m1xheader.png" width="1024" alt="header" />



<body>
<a name="top"></a>

<div id="content">
<h1> Magento for Developers: Part 2&mdash;The Magento Config</h1>
<p><em>by Alan Storm</em>, updated for Magento 1.12</p>
<p><a href="{{ site.m1xgithuburl }}/magefordev/mage-for-dev-2.html" target="_blank">Edit this page on GitHub <img src="{{ site.baseurl }}/common/images/github_icon.png" height="15px"></a></p>
<!-- Magento for Developers: Part 2&mdash;The Magento Config -->
<!-- magento-for-dev-part-2-the-magento-config -->

<!-- <style type="text/css">
#kb-entry .series {float: right; width: 175px; margin-left: 10px; font-size: 12px; }
#kb-entry .series ul {margin-left: 15px;}
</style>
<div class="r-box-rss series"> -->
<h5>Other articles in this series:</h5>
<ul>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-1.html">Part 1&mdash;Introduction to Magento</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-2.html">Part 2&mdash;The Magento Config</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-3.html">Part 3&mdash;Magento Controller Dispatch</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-4.html">Part 4&mdash;Magento Layouts, Blocks and Templates</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-5.html">Part 5&mdash;Magento Models and ORM Basics</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-6.html">Part 6&mdash;Magento Setup Resources</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-7.html">Part 7&mdash;Advanced ORM: Entity Attribute Value</a>
<li><a href="{{ site.m1xgdeurl }}magefordev/mage-for-dev-8.html">Part 8&mdash;Varien Data Collections</a>
</ul>
</div>
<p class="para1">The config is the beating heart of the Magento System.  It describes, in whole, almost any module, model, class, template, 等. than you'll need to access.  It's a level of abstraction that most PHP developers aren't used to working with, and while it adds development time in the form of confusion and head scratching, it also allows you an unprecedented amount of flexibility as far as overriding default system behaviors go.  </p>

<p>To start with,  we're going to create a Magento module that will let us view the system config in our web browser.  Follow along by copying and pasting the code below, it's worth going through on your own as a way to start getting comfortable with things you'll be doing while working with Magento, as well as learning key terminology.</p>

<h5>In this article...</h5>
<ul><li><a href="#1" title="Setting up a Module Directory Structure">Setting up a Module Directory Structure</a>
<li><a href="#2" title="Creating a Module Config">Creating a Module Config</a>
<li><a href="#3" title="What Am I Looking at?">What Am I Looking at?</a>
<li><a href="#4" title="Why Do I Care?">Why Do I Care?</a>
</ul>

<a name="1"></a>
<h2>Setting up a Module Directory Structure</h2>

<p>We're going to be creating a Magento module.  A module is a group of php and xml files meant to extend the system with new functionality, or override core system behavior.  This may meaning adding  additional data models to track sales information, changing the behavior of existing classes, or adding entirely new features.  </p>

<p>It's worth noting that most of the base Magento system is built using the same module system you'll be using.  If you look in </p>

<pre>app/code/core/Mage</pre>

<p>each folder is a separate module built by the Magento team.  Together, these modules form the community shopping cart system you're using.  Your modules should be placed in the following folder</p>

<pre>app/code/local/Packagename</pre>

<p>"Packagename" should be a unique string to Namespace/Package your code.  It's an unofficial convention that this should be the name of your company.  The idea is to pick a string that no one else in the world could possibly be using.</p>

<pre>app/code/local/Microsoft</pre>

<p>We'll use "Magentotutorial".</p>

<p>So, to add a module to your Magento system, create the following directory structure</p>

<pre>app/code/local/Magentotutorial/Configviewer/Block
app/code/local/Magentotutorial/Configviewer/controllers
app/code/local/Magentotutorial/Configviewer/etc
app/code/local/Magentotutorial/Configviewer/Helper
app/code/local/Magentotutorial/Configviewer/Model
app/code/local/Magentotutorial/Configviewer/sql</pre>

<p>You won't need all these folder for every module, but setting them all up now is a smart idea. </p>

<p>Next, there's two files you'll need to create.  The first, config.xml, goes in the etc folder you just created.  </p>

<pre>app/code/local/Magentotutorial/Configviewer/etc/config.xml</pre>

<p>The second file should be created at the following location</p>

<pre>app/etc/modules/Magentotutorial_Configviewer.xml</pre>

<p>The naming convention for this files is Packagename_Modulename.xml.</p>

<p>The config.xml file should contain the following XML.  Don't worry too much about what all this does for now, we'll get there eventually</p>

<pre>
&lt;config>
    &lt;modules>
        &lt;Magentotutorial_Configviewer>
            &lt;version>0.1.0&lt;/version>
        &lt;/Magentotutorial_Configviewer>
    &lt;/modules>
&lt;/config>
</pre>

<p>Finally, Magentotutorial_Configviewer.xml should contain the following xml.</p>

<pre>
&lt;config>
    &lt;modules>
        &lt;Magentotutorial_Configviewer>
            &lt;active>true&lt;/active>
            &lt;codePool>local&lt;/codePool>
        &lt;/Magentotutorial_Configviewer>
    &lt;/modules>
&lt;/config>
</pre>

<p>That's it. You now have a bare bones module that won't do anything, but that Magento will be aware of.  To make sure you've done everything right, do the following:
<ol>
<li>Clear your Magento cache</li>
<li>In the Magento Admin, go to <strong>System->Configuration->Advanced</strong></li>
<li>In the "Disable modules output" panel verify that Magentotutorial_Configviewer shows up</li>
</ol>

<p>Congratulations, you've built your first Magento module!</p>

<a name="2"></a>
<h2>Creating a Module Config</h2>

<p>Of course, this module doesn't do anything yet.  When we're done, our module will
<ol>
<li>Check for the existence of a "showConfig" query string variable</li>
<li>If showConfig is present, display our Magento config and halt normal execution</li>
<li>Check for the existence of an additional query string variable, showConfigFormat that will let us specify text or xml output.</li>
</ol>

<p>First, we're going to add the following &lt;global&gt; section to our config.xml file.</p>

<pre>
&lt;config>
    &lt;modules>...&lt;/modules>
    &lt;global>
        &lt;events>
            &lt;controller_front_init_routers>
                &lt;observers>
                    &lt;Magentotutorial_configviewer_model_observer>
                        &lt;type>singleton&lt;/type>
                        &lt;class>Magentotutorial_Configviewer_Model_Observer&lt;/class>
                        &lt;method>checkForConfigRequest&lt;/method>
                    &lt;/Magentotutorial_configviewer_model_observer>
                &lt;/observers>
            &lt;/controller_front_init_routers>
        &lt;/events>
    &lt;/global>
&lt;/config>
</pre>

<p>Then, create a file at </p>

<pre>Magentotutorial/Configviewer/Model/Observer.php</pre>

<p>and place the following code inside</p>

<pre>
&lt;?php
    class Magentotutorial_Configviewer_Model_Observer {
        const FLAG_SHOW_CONFIG = 'showConfig';
        const FLAG_SHOW_CONFIG_FORMAT = 'showConfigFormat';

        private $request;

        public function checkForConfigRequest($observer) {
            $this-&gt;request = $observer-&gt;getEvent()-&gt;getData('front')-&gt;getRequest();
            if($this-&gt;request-&gt;{self::FLAG_SHOW_CONFIG} === 'true'){
                $this-&gt;setHeader();
                $this-&gt;outputConfig();
            }
        }

        private function setHeader() {
            $format = isset($this-&gt;request-&gt;{self::FLAG_SHOW_CONFIG_FORMAT}) ?
            $this-&gt;request-&gt;{self::FLAG_SHOW_CONFIG_FORMAT} : 'xml';
            switch($format){
                case 'text':
                    header("Content-Type: text/plain");
                    break;
                default:
                    header("Content-Type: text/xml");
            }
        }

        private function outputConfig() {
            die(Mage::app()-&gt;getConfig()-&gt;getNode()-&gt;asXML());
        }
    }
</pre>

<p>That's it.  Clear your Magento cache again and then load any Magento URL with a <tt>showConfig=true</tt> query string</p>

<pre>http://magento.example.com/?showConfig=true</pre>

<a name="3"></a>
<h2>What am I looking at?</h2>

<p>You should be looking at a giant XML file.  This describes the state of your Magento system.  It lists all modules, models, classes, event listeners or almost anything else you could think of.</p>

<p>例如， consider the config.xml file you created above.  If you search the XML file in your browser for the text <tt>Configviewer_Model_Observer</tt> you'll find your class listed.  Every module's config.xml file is parsed by Magento and included in the global config.</p>

<a name="4"></a>
<h2>Why Do I Care?</h2>

<p>Right now this may seem esoteric, but this config is key to understanding Magento.  Every module you'll be creating will add to this config, and anytime you need to access a piece of core system functionality, Magento will be referring back to the config to look something up.  </p>

<p>A quick example:  As an MVC developer, you've likely worked with some kind of helper class, instantiated something like </p>

<pre>
$helper_sales = new HelperSales();
</pre>

<p>One of the things Magento has done is abstract away PHP's class declaration.  In Magento, the above code looks something like </p>

<pre>
$helper_sales = Mage::helper('sales');
</pre>

<p>In plain english, the static helper method will:
<ol>
<li>Look in the &lt;helpers /&gt; section of the Config.</li>
<li>Within &lt;helpers /&gt;, look for a &lt;sales /&gt; section</li>
<li>Within the &lt;sales /&gt; section look for a &lt;class /&gt; section</li>
<li>Append the part after the slash to the value found in #3 (defaulting to <tt>data</tt> in this case)</li>
<li>Instantiate the class found in #4 (Mage_Sales_Helper_Data)</li>
</ol>

<p>While this seems like a lot of work (and it is), the key advantage is by always looking to the config file for class names, we can override core Magento functionality <strong>without</strong> changing or adding to the core code.  This level of meta programming, not usually found in PHP, allows you to cleanly extend only the parts of the system you need to.</p>
